/*
 * ==========================================================================*\
 * | $Id$
 * |*-------------------------------------------------------------------------*|
 * | Copyright (C) 2010-2011 Virginia Tech | | This file is part of CloudSpace.
 * | | CloudSpace is free software; you can redistribute it and/or modify | it
 * under the terms of the GNU General Public License as published | by the Free
 * Software Foundation; either version 3 of the License, or | (at your option)
 * any later version. | | CloudSpace is distributed in the hope that it will be
 * useful, | but WITHOUT ANY WARRANTY; without even the implied warranty of |
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | GNU General
 * Public License for more details. | | You should have received a copy of the
 * GNU General Public License | along with CloudSpace; if not, see
 * <http://www.gnu.org/licenses/>.
 * \*==========================================================================
 */

package cloudspace.util;

import org.webcat.exceptiondoctor.ExceptionDoctor;
import org.zkoss.zhtml.Text;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.util.GenericComposer;

import student.web.internal.Exceptions;
import bsh.ParseException;


// -------------------------------------------------------------------------
/**
 * A controller to go along with the WEB-INF/sys/error.zul page. It fills in the
 * text contents of the main window on the error page.
 * 
 * @author Mike Woods
 * @author Last changed by $Author$
 * @version $Revision$, $Date: 2009-11-05$
 */
public class ErrorController extends GenericComposer
{
    // ~ Instance/static variables .............................................

    private static final long serialVersionUID = -1664663247569051114L;


    // ~ Methods ...............................................................

    // ----------------------------------------------------------
    @Override
    public void doAfterCompose( Component comp ) throws Exception
    {
        Throwable error = (Throwable)Executions.getCurrent()
            .getAttribute( "javax.servlet.error.exception" );
        error = Exceptions.getRealCause( error );
        cloudspace.stats.StatisticsService.getStatService()
            .observe( new cloudspace.stats.StatEvent( "ExceptionLastFired" ) );

        if ( error instanceof ParseException )
        {
            ParseException pe = (ParseException)error;
            try
            {
                String source = pe.getErrorSourceFile();
                if ( source != null
                    && source.matches( "inline evaluation of:\\s+1: _[^\\.]*\\.bean = .*;\\s*" ) )
                {
                    // Reset the message text to include the source line
                    pe.setMessage( "Sourced file: " + source + " : "
                        + pe.getMessage() );
                }
            }
            catch ( Exception e )
            {
                // ignore it
            }
        }

        try
        {
            StackTraceElement[] element = error.getStackTrace();
            if(element != null && element.length> 0 && !element[0].getClassName().equals( "bsh.Reflect" ))
                error = ExceptionDoctor.addExplanation( error );
        }
        catch ( Exception e )
        {
            // ignore it
        }

        ( (Text)comp.getFellow( "exceptionName" ) ).setValue( Exceptions.simplifiedExceptionName( error ) );

        // String message = Exceptions.escapeHtml(error.getMessage());
        String message = Exceptions.highlightExceptionMessage( error );
        comp.getFellow( "messageRow" ).setVisible( message != null );
        ( (Text)comp.getFellow( "messageText" ) ).setValue( message );

        String explanation = Exceptions.explain( error );
        comp.getFellow( "explanationRow" ).setVisible( explanation != null );
        ( (Text)comp.getFellow( "explanationText" ) ).setValue( explanation );

        String simpleTrace = Exceptions.escapeHtml( Exceptions.simplifiedStackTrace( error ) );
        comp.getFellow( "simpleTraceWindow" ).setVisible( simpleTrace != null );
        ( (Text)comp.getFellow( "simpleTraceText" ) ).setValue( simpleTrace );

        ( (Text)comp.getFellow( "fullExceptionName" ) ).setValue( error.getClass()
            .getName() );
        String rawMessage = error.getMessage();
        if ( rawMessage == null )
        {
            rawMessage = "null";
        }
        else
        {
            rawMessage = Exceptions.escapeHtml( rawMessage );
        }
        ( (Text)comp.getFellow( "rawMessage" ) ).setValue( rawMessage );
        ( (Text)comp.getFellow( "stackTraceText" ) ).setValue( Exceptions.escapeHtml( Exceptions.fullStackTrace( error ) ) );

    }
}
